package com.qingfeng.client.service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.qingfeng.Config;
import com.qingfeng.base.exception.LsException;
import com.qingfeng.client.bean.Seller;
import com.qingfeng.client.bean.StorePayType;
import com.qingfeng.client.dao.SellerMapper;
import com.qingfeng.client.dao.StorePayTypeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 商家注册服务
 *
 * @Author ff
 * @Data 2017/10/24 20:22
 */
@Service
public class SellerService {

    Logger logger = LoggerFactory.getLogger(this.getClass().getName());


    @Resource
    private SellerMapper sellerDao;

    @Resource
    private StorePayTypeMapper storePayTypeDao;

    /**
     * 商家注册
     *
     * @param sellerBean
     * @return 卖家id
     */
    public Integer register(Seller sellerBean) {
        logger.info("商家注册：" + sellerBean.toString());
        if (sellerBean.getName() == null || "".equals(sellerBean.getName())) {
            sellerBean.setName(sellerBean.getPhone());
        }
        if (sellerBean.getCountryPhoneCode() == null || "".equals(sellerBean.getCountryPhoneCode())) {
            sellerBean.setCountryPhoneCode("+86");
        }
        if (sellerBean.getSex() > 0) {
            sellerBean.setSex(1);
        } else {
            sellerBean.setSex(0);
        }
        Integer id = null;
        id = sellerDao.insert(sellerBean);
        return id;
    }

    /**
     * 登录，用户名，或者手机号登录
     *
     * @param userName 用户名，手机号
     * @param password 密码
     * @return 商家对象，不存在为null
     */
    public Seller login(String userName, String password, String countryCode) {
        MDC.put("username", userName);
        MDC.put("password", password);
//        dbLogger.info("用户登录");
        if (countryCode == null) {
            countryCode = "+86";
        }
        try {
            if (isMobileNO(userName)) {
                return sellerDao.getByPhoneAndPassword(userName, password, countryCode);
            } else {
                return sellerDao.getByNameAndPassword(userName, password);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List getPayType(Long storeId) {
        List<Map<String, Object>> storePayTypeBeans = storePayTypeDao.queryByStoreId(storeId);
        return storePayTypeBeans;
    }


    /**
     * 判断是否是11位数字的手机号
     *
     * @param mobiles
     * @return
     */
    public boolean isMobileNO(String mobiles) {
        Pattern p = Pattern.compile("\\d{11}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    /*=================================================================未完成===========================================================================*/

    /**
     * 检测姓名，和证件号是否正确对应，调用第三方接口。第三方接口，我只找到ali付费的，
     *
     * @param realName
     * @param authType
     * @param authAccount
     * @return
     */
    public boolean authentication(String realName, int authType, String authAccount) {
        switch (authType) {
            case Config.AUTH_IDENTITY://1身份证认证
                break;
            case Config.AUTH_PASSPORT://2护照
                break;
            case Config.AUTH_HONGKONG:// 3港台回乡证
                break;
            case Config.AUTH_TAIWAN://4港澳台居民证
                break;
            default://其他
                break;
        }
        return true;
    }

    /*=====================================================================================================================================*/

    public static String msg = StringUtils.EMPTY;

    @Transactional
    public String saveAuthInfo(Seller sellerBean, StorePayType storePayTypeBean) {
        msg = StringUtils.EMPTY;

        msg = "更新个人信息失败";
        int result = sellerDao.updateRealNameAndAuthTypeAndAuthAccountById(sellerBean, sellerBean.getId());
        if (result>0) {
            msg = "该支付方式已被绑定";
        } else {
            throw new LsException(0,Thread.currentThread().getStackTrace()[2].getFileName() + "\nexception:" + sellerBean.toString());
        }
        Integer id = storePayTypeDao.insert(storePayTypeBean);
        if (id == null || id <= 0) {
            throw new LsException(0,Thread.currentThread().getStackTrace()[2].getFileName() + "\nexception:" + storePayTypeBean.toString());
        } else {
            msg = StringUtils.EMPTY;
        }
        return msg;
    }

    public boolean updatePassword(String phone, String password) {
        int i= sellerDao.updatePasswordByPhone(password,phone);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

    public boolean removeUser(String phone, String password) {
        int i =sellerDao.deleteByPhoneAndPassword(phone, password);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

}
